package objetos

import scala.collection.mutable.ArrayBuffer
import org.joda.time.Interval
import org.joda.time.LocalDate
import traits.Cancha

object informe {

  def reservasPorCancha(cancha: Cancha): Int = {
    Reservas.canchas(cancha).size
  }

  def reservasCanchaPorDia(array: ArrayBuffer[Interval], dia: LocalDate): Boolean = {
    array.exists(f => f.getStart.toLocalDate.equals(dia))
  }

  def reservasPorDia(dia: LocalDate): Iterable[Cancha] = {
    (Reservas.canchas.keys.filter(g => reservasCanchaPorDia(Reservas.canchas(g), dia)))
  }

  def canchaConMasReservas: List[Cancha] = {
    val max = Reservas.canchas.keys.map(f => reservasPorCancha(f)).fold(0)((x, y) => Math.max(x, y))
    if (max != 0) Reservas.canchas.keys.filter(p => reservasPorCancha(p) == max).toList else Nil
  }

  def canchaLibre(horario: Interval): Cancha = {
    Reservas.canchas.keys.find(f => Reservas.canchas(f).filter(g => horario.overlaps(g)).isEmpty).getOrElse(null)
  }

  def facturacionPorCancha(cancha: Cancha): BigDecimal = {
    val lista = Reservas.canchas(cancha).map(f => cancha.calcularPrecio(f.getEnd()))
    lista.fold(BigDecimal(0))((acum, sig) => acum + sig)
  }

  def facturacionTotal: BigDecimal = {
    val lista = Reservas.canchas.keys.map(f => facturacionPorCancha(f))
    lista.fold(BigDecimal(0))((acum, sig) => acum + sig)
  }

  def listaReservasDelDia(dia: LocalDate): Iterable[Interval] = {
    Reservas.canchas.values.flatten.filter(f => f.getStart.toLocalDate.equals(dia))
  }
}